package com.gioneco.util.esCondition;

import com.gioneco.model.PageResultVO;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Es  分页对象封装类
 * @author dongjialin
 * @since 2022-06-01
 */
public class ConversionDoUtil{


    /**
     * ES 分页封装
     */
    public static <K> PageResultVO<K> pagingHandler(SearchHits<K> searchHits, Query query) {
        PageResultVO<K> pageResultVO = new PageResultVO();
        pageResultVO.setRecords(searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList()));
        pageResultVO.setPages((long) Math.ceil((double) searchHits.getTotalHits() /query.criteria.size));
        pageResultVO.setTotal(searchHits.getTotalHits());
        pageResultVO.setSize(query.criteria.size.longValue());
        pageResultVO.setCurrent(query.criteria.current.longValue());
        return pageResultVO;
    }

    /**
     * ES List
     */
    public static <K> List list(SearchHits<K> searchHits) {
        return searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
    }


}
